<?php
/**
 * Class for livechat tracking table. This is the main data table. Does not
 * contain chat dialogue
 *
 * @access 	public
 * @author      Rob
 * @copyright	ResMania 2009-present all rights reserved.
 * @version	1.0
 * @link	http://docs.resmania.com/index.php/LiveChat_Module
 * @since  	09-2010
 */
class RM_LiveChat extends RM_Model_Flexible {

    protected $_name = 'rm_livechat';

    /**
     * fetch all livechat entries based on the chat ID
     *
     * @param  string  chat_id
     * @return object  dataset
     */
    public function fetchAll($chat_id) {
        $sql = "
            SELECT
                *
            FROM
                " . $this->_name . "
            WHERE
                id='" . $chat_id . "'
            ";

        return $this->_getBySQL($sql);
    }

    /**
     * fetch all open livechat entries that are not assigned to an operator
     * and not marked as closed
     *
     * @return object  dataset
     */
    public function fetchAllOpen() {
        $sql = "
            SELECT
                *
            FROM
                " . $this->_name . "
            WHERE
                closed='0' AND operator_id=''
            ";

        return $this->_getBySQL($sql);
    }

    /**
     * set a chat to closed based on the chat ID
     *
     * @param  string  chat_id
     * @return object
     */
    public function closeChat($chat_id) {
        $data["closed"] = 1;
        $where = "id='" . $chat_id . "'";

        return parent::update($data, $where);
    }

    /**
     * Accept a chat: this assigns a chat to the operator.
     *
     * @todo When user groups are added we will need to remove the hard coded
     * operator id (1) and set this to the currently logged on administrator ID
     * @param  string  chat_id
     * @return object
     */
    public function acceptChat($chat_id) {

        $data["operator_id"] = 1;
        $where = "id='" . $chat_id . "'";

        return parent::update($data, $where);
    }

    /**
     * this updates the livechat table with the last update time. This is used
     * to check if chats are answered and later will be used for pruning
     * old chats
     *
     * @todo need to add better pruning based on this time and date
     * @param  string  chat_id
     * @return object
     */
    public function updateLastUpdate($chat_id) {
        $data["last_update"] = date("Y-m-d h:i:s");
        $where = "id='" . $chat_id . "'";

        return parent::update($data, $where);
    }

    /**
     * get User Information based on the chat ID
     *
     * @param  string  chat_id
     * @return object  dataset
     */
    public function getUserInfo($chat_id) {
        $sql = "
            SELECT
                *
            FROM
                " . $this->_name . "
            WHERE
                id='" . $chat_id . "'
            ";

        return $this->_getBySQL($sql);
    }

    /**
     * get the isTyping Status
     *
     * @param operator  boolean
     */
    public function getIsTyping($operator = true, $chat_id){

        if ($operator){
            $select = "operator_istyping";
        } else {
            $select = "user_istyping";
        }

        $sql = "
            SELECT
                $select
            FROM
                " . $this->_name . "
            WHERE
                id='" . $chat_id . "'
            LIMIT 1
            ";

        return $this->_getBySQL($sql);
    }

    /**
     * set the is Typing status
     */
    public function setIsTyping($chat_id, $operator = true, $status = 0){
        if ($operator){
            $data["operator_istyping"] = $status;
        } else {
            $data["user_istyping"] = $status;
        }
        $where = "id='" . $chat_id . "'";
        return parent::update($data, $where);
    }
}